package com.gu.controller;

import com.github.pagehelper.PageInfo;
import com.gu.VO.LayuiVO;
import com.gu.VO.ServerResponse;
import com.gu.entity.NoticeDO;
import com.gu.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.controller
 * @ClassName: NoticeController
 * @Author: gusongliao
 * @Description: 公告控制器
 * @createDate 2021/03/18 17:13
 */

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/noticeList")
    @ResponseBody
    public LayuiVO listNotice(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,String searchParam, String searchDate) {
        PageInfo notice = noticeService.getNoticeBySearchParam(pageNum, pageSize, searchParam, searchDate);
        return LayuiVO.success(notice.getTotal(),notice.getList());
    }

    @PostMapping("/insertNotice")
    @ResponseBody
    public ServerResponse insertNotice(@RequestBody NoticeDO noticeDO) {
        return noticeService.insertNotice(noticeDO);
    }

    @RequestMapping("/viewNotice")
    public String viewNotice(Integer id, HttpServletRequest request) {
        System.out.println("********************************");
        System.out.println(id);
        System.out.println("********************************");
        NoticeDO notice = noticeService.getById(id);
        request.setAttribute("content",notice.getContent());
        request.setAttribute("title",notice.getTitle());
        return "viewNotices";
    }

    @RequestMapping("/viewNoticeUser")
    public String viewNoticeUser(Integer id, HttpServletRequest request) {
        System.out.println("********************************");
        System.out.println(id);
        System.out.println("********************************");
        NoticeDO notice = noticeService.getById(id);
        request.setAttribute("content",notice.getContent());
        request.setAttribute("title",notice.getTitle());
        return "viewNoticesUser";
    }

    @GetMapping("/deleteNotice")
    @ResponseBody
    public ServerResponse deleteNotice(Integer[] id) {
        return noticeService.deleteByIds(id);
    }

    @RequestMapping("/notice")
    public String notice(){
        return "handleNotices";
    }




}
